package com.yc.cloud.admin.service.impl;

import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.admin.dto.request.HwCameraRecordingLogCreateReqDTO;
import com.yc.cloud.admin.dto.request.HwCameraRecordingLogUpdateReqDTO;
import com.yc.cloud.entity.HwCameraRecordingLog;
import com.yc.cloud.mapper.HwCameraRecordingLogMapper;
import com.yc.cloud.admin.service.HwCameraRecordingLogService;
import com.yc.cloud.admin.vo.HwCameraRecordingLogVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.common.basic.exception.Asserts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.StrUtil;
import java.util.Objects;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 摄像头录制任务执行日志表 服务实现类
 * </p>
 *
 * @author kinggu
 * @since 2025-09-15
 */
@Service
public class HwCameraRecordingLogServiceImpl extends ServiceImpl<HwCameraRecordingLogMapper, HwCameraRecordingLog> implements HwCameraRecordingLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HwCameraRecordingLogVo create(HwCameraRecordingLogCreateReqDTO createReqDTO) {
        HwCameraRecordingLog entity = new HwCameraRecordingLog();
        BeanUtils.copyProperties(createReqDTO, entity);
        boolean success = save(entity);
        if (success) {
            return getVOById(entity.getId());
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HwCameraRecordingLogVo update(HwCameraRecordingLogUpdateReqDTO updateReqDTO) {
        // 1. 校验待更新的记录是否存在
        if (getById(updateReqDTO.getId()) == null) {
            Asserts.fail("记录不存在，无法更新");
        }
        HwCameraRecordingLog entity = new HwCameraRecordingLog();
        BeanUtils.copyProperties(updateReqDTO, entity);
        boolean success = updateById(entity);
        if (success) {
            return getVOById(entity.getId());
        }
        return null;
    }

    @Override
    public CommonPage<HwCameraRecordingLogVo> list(BasePageReqDTO pageReqDTO) {
        QueryWrapper<HwCameraRecordingLog> queryWrapper = new QueryWrapper<>();

        // 排序
        if (StrUtil.isNotEmpty(pageReqDTO.getSortBy())) {
            // 注意：pageReqDTO.getSortBy() 需要是数据库中的列名（驼峰转下划线），请注意防护SQL注入
            if (Objects.equals(BasePageReqDTO.ORDER_DESC, pageReqDTO.getOrder())) {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(pageReqDTO.getSortBy()));
            } else {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(pageReqDTO.getSortBy()));
            }
        }

        // 关键字模糊查询
        if (StrUtil.isNotEmpty(pageReqDTO.getKeyword())) {
            // TODO: 请根据业务需求，修改需要模糊查询的字段，以下为示例
            // queryWrapper.and(wrapper -> wrapper.like("name", pageReqDTO.getKeyword())
            //                                   .or()
            //                                   .like("description", pageReqDTO.getKeyword()));
        }
        
        // 根据 page 参数决定是否分页
        if (Boolean.TRUE.equals(pageReqDTO.getPage())) {
            Page<HwCameraRecordingLog> page = new Page<>(pageReqDTO.getPageNum(), pageReqDTO.getPageSize());
            Page<HwCameraRecordingLog> dbPage = baseMapper.selectPage(page, queryWrapper);
            IPage<HwCameraRecordingLogVo> voPage = dbPage.convert(this::convertToVo);
            return CommonPage.restPage(voPage);
        } else {
            List<HwCameraRecordingLog> allRecords = baseMapper.selectList(queryWrapper);
            CommonPage<HwCameraRecordingLogVo> result = new CommonPage<>();
            result.setList(convertToVoList(allRecords));
            result.setTotal((long) allRecords.size());
            result.setPageNum(1);
            result.setPageSize(allRecords.size());
            result.setTotalPage(1);
            return result;
        }
    }

    @Override
    public HwCameraRecordingLogVo getVOById(Long id) {
        HwCameraRecordingLog entity = getById(id);
        return convertToVo(entity);
    }

    @Override
    public List<HwCameraRecordingLogVo> convertToVoList(List<HwCameraRecordingLog> entityList) {
        if (entityList == null) {
            return null;
        }
        return entityList.stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
    }

    private HwCameraRecordingLogVo convertToVo(HwCameraRecordingLog entity) {
        if (entity == null) {
            return null;
        }
        HwCameraRecordingLogVo vo = new HwCameraRecordingLogVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public HwCameraRecordingLog get(Long id) {
        return getById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        // 1. 校验待删除的记录是否存在
        if (getById(id) == null) {
            Asserts.fail("记录不存在，无法删除");
        }
        return removeById(id);
    }
} 